package com.topc.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topc.base.constant.RedisConstant;
import com.topc.base.exception.Asserts;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.core.sevice.RedisService;
import com.topc.platform.mapper.AnnouncementInfoMapper;
import com.topc.platform.model.po.AnnouncementInfo;
import com.topc.platform.model.vo.AnnouncementShowVo;
import com.topc.platform.model.vo.AnnouncementTitleShowVo;
import com.topc.platform.service.IAnnouncementInfoService;
import com.topc.platform.model.dto.SaveAnnouncementDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 公告信息 服务实现类
 * </p>
 *
 * @author Gogoing
 */
@Slf4j
@Service
public class AnnouncementInfoServiceImpl extends ServiceImpl<AnnouncementInfoMapper, AnnouncementInfo> implements IAnnouncementInfoService {


    @Autowired
    private RedisService redisService;

    @Override
    public PageResult<AnnouncementShowVo> searchInfoByPage(PageParams pageParams, String title, int status) {
        LambdaQueryWrapper<AnnouncementInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(AnnouncementInfo::getId, AnnouncementInfo::getTitle, AnnouncementInfo::getStatus, AnnouncementInfo::getCreateTime)
                .like(ObjectUtil.isNotEmpty(title), AnnouncementInfo::getTitle, title)
                .eq(ObjectUtil.notEqual(status,-1), AnnouncementInfo::getStatus, status);
        Page<AnnouncementInfo> page=this.page(pageParams.page(), queryWrapper);
        List<AnnouncementShowVo> collect = page.getRecords().stream().map(announcement -> {
            AnnouncementShowVo announcementShowVo = new AnnouncementShowVo();
            BeanUtil.copyProperties(announcement, announcementShowVo);
            int clickCount = redisService.getCount(RedisConstant.ANNOUNCEMENT_CLICK_COUNT + announcement.getId());
            announcementShowVo.setClickCount(clickCount);
            return announcementShowVo;
        }).collect(Collectors.toList());
        PageResult pageResult = PageUtils.pageToResult(page);
        pageResult.setItems(collect);
        return pageResult;
    }


    @Override
    public String getContentById(Long id){
        AnnouncementInfo announcementInfo =this.getById(id);
        if(ObjectUtil.isEmpty(announcementInfo)) Asserts.fail("id不存在");
        return announcementInfo.getContent();
    }


    @Override
    public void saveInfo(SaveAnnouncementDto saveAnnouncementDto){
        AnnouncementInfo announcementInfo =new AnnouncementInfo();
        announcementInfo.setContent(saveAnnouncementDto.getContent());
        announcementInfo.setTitle(saveAnnouncementDto.getTitle());
        announcementInfo.setStatus(saveAnnouncementDto.getStatus());
        int insert =this.getBaseMapper().insert(announcementInfo);
        if(ObjectUtil.equal(insert,0)) Asserts.fail("新增失败");
    }

    @Override
    public void updateInfoById(Long id, SaveAnnouncementDto saveAnnouncementDto){
        AnnouncementInfo announcementInfo =new AnnouncementInfo();
        announcementInfo.setContent(saveAnnouncementDto.getContent());
        announcementInfo.setTitle(saveAnnouncementDto.getTitle());
        announcementInfo.setStatus(saveAnnouncementDto.getStatus());
        announcementInfo.setId(id);
        boolean update =this.updateById(announcementInfo);
        if(!update) Asserts.fail("更新失败");
    }

    @Override
    public void removeInfoById(Long id){
        boolean remove =this.removeById(id);
        if(!remove) Asserts.fail("删除失败");
    }

    @Override
    public AnnouncementShowVo getDetailById(Long id) {
        AnnouncementInfo announcementInfo = this.getById(id);
        if (ObjectUtil.isEmpty(announcementInfo)) Asserts.fail("删除失败");
        AnnouncementShowVo announcementShowVo = new AnnouncementShowVo();
        BeanUtil.copyProperties(announcementInfo, announcementShowVo);
        int clickCount = redisService.getCount(RedisConstant.ANNOUNCEMENT_CLICK_COUNT + announcementInfo.getId());
        redisService.incr(RedisConstant.ANNOUNCEMENT_CLICK_COUNT + announcementInfo.getId(),1);
        announcementShowVo.setClickCount(clickCount);
        return announcementShowVo;
    }

    @Override
    public AnnouncementInfo getInfoById(Long id) {
        AnnouncementInfo announcementInfo = this.getById(id);
        if (ObjectUtil.isEmpty(announcementInfo)) Asserts.fail("查询失败");
        return announcementInfo;
    }

    @Override
    public List<AnnouncementTitleShowVo> searchTitle() {
        LambdaQueryWrapper<AnnouncementInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.notLike(AnnouncementInfo::getStatus, -1)
                .orderByDesc(AnnouncementInfo::getUpdateTime);
        List<AnnouncementTitleShowVo> announcementTitleShowVos = new ArrayList<>();
        List<AnnouncementInfo> list = this.list(queryWrapper);
        for (AnnouncementInfo announcementInfo: list){
            AnnouncementTitleShowVo announcementTitleShowVo = new AnnouncementTitleShowVo();
            announcementTitleShowVo.setId(announcementInfo.getId());
            announcementTitleShowVo.setTitle(announcementInfo.getTitle());
            announcementTitleShowVo.setUpdateTime(announcementInfo.getUpdateTime());
            announcementTitleShowVos.add(announcementTitleShowVo);
        }
        return announcementTitleShowVos;
    }
}

